package com.ybt.heartratem.common.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ybt.heartratem.R;
import com.ybt.heartratem.common.app.CacheManager;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;

import me.goldze.mvvmhabit.http.DownLoadManager;
import me.goldze.mvvmhabit.http.download.ProgressCallBack;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.constant.Contact;
import z.hol.shellandroid.ShellAndroid;

/**
 * 版本检查提示弹出框
 */
public class CheckVersionDialog extends Dialog {
    private Context mContext;
    private View mainView;
    private TextView mTitleTV, mContentTV, mNextTV, mCancelTV, mProgressRateTV;
    private ProgressBar mDownloadProgressBar;
    private LinearLayout mProgressLL;
    private int versionCode;
    private String url;
    private OperatorListener listener;
    private boolean isCancel = false;

    public CheckVersionDialog(Context context) {
        super(context, R.style.Theme_Hold_Dialog_Base);
        setCanceledOnTouchOutside(true);
        mContext = context;
        init();
    }

    public CheckVersionDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        setCanceledOnTouchOutside(true);
        init();
    }

    protected CheckVersionDialog(Context context, boolean cancelable,
                                 OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
        setCanceledOnTouchOutside(true);
        init();
    }

    private void init() {
        mainView = LayoutInflater.from(mContext).inflate(R.layout.dialog_version_upload_layout, null);
        mCancelTV = mainView.findViewById(R.id.cancel_tv);
        mTitleTV = mainView.findViewById(R.id.title_tv);
        mContentTV = mainView.findViewById(R.id.content_tv);
        mProgressRateTV = mainView.findViewById(R.id.progress_rate_tv);
        mProgressLL = mainView.findViewById(R.id.progress_ll);
        mNextTV = mainView.findViewById(R.id.sure_tv);
        mDownloadProgressBar = mainView.findViewById(R.id.download_progress_bar);
        mCancelTV.setOnClickListener(v -> {
            isCancel = true;
            dismiss();
        });
        mNextTV.setOnClickListener(view -> {

        });
        setContentView(mainView);
    }

    @Override
    public void show() {
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        super.show();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = Contact.DISPLAYH;
        window.setAttributes(lp);
    }

    public void setItem(int versionCode, String url, OperatorListener listener) {
        this.listener = listener;
        this.versionCode = versionCode;
        this.url = url;
        mTitleTV.setText("最新版本V" + versionCode);
        mProgressLL.setVisibility(View.VISIBLE);
        mCancelTV.setVisibility(View.GONE);
        mNextTV.setVisibility(View.GONE);
        setCancelable(false);

        DownLoadManager.getInstance().load(url, new ProgressCallBack(CacheManager.SD_APK_DIR, "ybt-dzc-gadon.apk") {
            @Override
            public void onSuccess(Object o) {
                if (!isCancel) {
                    File file = new File(CacheManager.SD_APK_DIR + File.separator + "ybt-dzc-gadon.apk");
                    ShellAndroid mShell = new ShellAndroid(null);
                    String flagFile = mShell.initFlagMinimum(mContext);
                    mShell.printOutput();
                    mShell.setFlagFile(flagFile);
                    mShell.checkRoot();
                    mShell.exec(true, "pm install -r " + file.getAbsolutePath());
                    KLog.e("ApkInstall", "执行安装文件：" + file.getAbsolutePath());
                }
                dismiss();
            }

            @Override
            public void progress(long progress, long total) {
                if (!isCancel) {
                    mDownloadProgressBar.setMax((int) total);
                    mDownloadProgressBar.setProgress((int) progress);
                    int rate = new BigDecimal(progress + "")
                            .divide(new BigDecimal(total + ""), 2, RoundingMode.HALF_UP)
                            .multiply(new BigDecimal(100)).intValue();
                    mProgressRateTV.setText(rate + "%");
                }
            }

            @Override
            public void onError(Throwable e) {

            }
        });
    }

    public interface OperatorListener {
        void operator();
    }
}
